<?php
    function connect() {
        $servername = "localhost";
        $username = "bcoh";
        $password = "mysql4bcoh";

        $argc = func_num_args();
        
        // 创建连接
        if ($argc == 1)
            $conn = new mysqli($servername, $username, $password, func_get_arg(0));
        else if ($argc == 0)
            $conn = new mysqli($servername, $username, $password);

        // 检测连接
        if (!$conn)
            die("Connection failed: " . mysqli_connect_error());

        //设置编码
        mysqli_query($conn , "set names utf8");

        return $conn;
    }

    function query($conn, $sql) {
        $argc = func_num_args();

        if ($argc == 2)
            return mysqli_query($conn, $sql);
        else if ($argc == 3)
            return mysqli_query($conn, $sql, func_get_arg(2));
    }

    function select($conn, $table, $column, $clause) {
        return query($conn, "SELECT $column FROM $table WHERE $clause");
    }

    function update($conn, $table, $setting, $clause) {
        return query($conn, "UPDATE $table SET $setting WHERE $clause");
    }

    //插入数据到数据库
    //MySQL连接  数据表名  插入位置(省略即为按顺序插入全部数据)  插入数据
    function insert($conn) {
        $argc = func_num_args();

        if ($argc == 4)
            return query($conn, "INSERT INTO " . func_get_arg(1) . " (" . func_get_arg(2) . ") VALUES (" . func_get_arg(3) . ")");
        else if ($argc == 3)
            return query($conn, "INSERT INTO " . func_get_arg(1) . "  VALUES (" . func_get_arg(2) . ")");
    }

    function close($conn) {
        mysqli_close($conn);
    }
?>